<!-- 获取权限表单弹窗 -->
<form id="role-form" lay-filter="role-form" class="layui-form model-form">
    <input name="per_id" type="hidden"/>

    <div class="layui-form-item">
        <label class="layui-form-label">权限</label>
        <div class="layui-input-block">
            <select name="perIds" xm-select="perIds" lay-verify="required">
            </select>
        </div>
    </div>

    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="role-form-submit" lay-submit>保存</button>
    </div>
</form>

<script>
    layui.use(['layer', 'admin', 'form', 'formSelects','treetable'], function () {
        var layer = layui.layer;
        var admin = layui.admin;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var treetable = layui.treetable;

        // 获取所有权限
        layer.load(2);
        admin.req('api-sys/api/per/getlist?tree=true', {}, function (data) {
            layer.closeAll('loading');
            if (0 == data.code) {
                // 渲染多选下拉框
                var perSelectData = new Array();
                for (var i = 0; i < data.data.length; i++) {
                    perSelectData.push({name: data.data[i].permissionName, value: data.data[i].permission_id});
                }
                formSelects.data('perIds', 'local', {arr: perSelectData});

                // 回显per数据
                var per = admin.getTempData('t_per');
                console.log(per);
                $('#role-form').attr('method', 'POST');
                if (per) {
                    form.val('role-form', per);
                    $('#role-form').attr('method', 'POST');
                    var rds = new Array();
                    for (var i = 0; i < per.permissions.length; i++) {
                        rds.push(sysUser.permissions[i].permission_id);
                    }
                    formSelects.value('perIds', rds);
                }

            } else {
                layer.msg('获取角色失败', {icon: 2, time: 500});
            }
        }, 'GET');

        // 表单提交事件
        form.on('submit(role-form-submit)', function (data) {
            layer.load(2);
            data.roleId = data.field.id;
            data.authIds = data.field.perIds.split(",");
            console.log(data);
            admin.req('api-sysUser/api/permissions/granted', JSON.stringify(data), function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    layer.msg(data.msg, {icon: 1, time: 500});
                    admin.finishPopupCenter();
                } else {
                    layer.msg(data.msg, {icon: 2, time: 500});
                }

            }, $('#role-form').attr('method'));
            return false;
        });
    });
</script>